package com.fyzk.utils.jc;

import com.fyzk.activity.lottery.jc.beidan.BeiDanChildBean;
import com.fyzk.activity.lottery.jc.football.JZBetNumUtil;
import com.fyzk.activity.lottery.jc.football.SpValueBean;
import com.fyzk.utils.LogUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * @mood
 * @description
 * @author Administrator
 * @date 2018/6/10 10:53
 * </p>
 */
public class BdGroupUtil {

    /**
     * Java bean对象克隆
     * @param obj
     * @param <T>
     * @return
     */
    public static <T extends Serializable> T clone(T obj){
        T clonedObj = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);
            oos.close();
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bais);
            clonedObj = (T) ois.readObject();
            ois.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return clonedObj;
    }

    public static Map<String, ArrayList<BeiDanChildBean>> getMapInfoData(ArrayList<BeiDanChildBean> infos) {
        Map<String, ArrayList<BeiDanChildBean>> mapInfoData = new HashMap<String, ArrayList<BeiDanChildBean>>();
        for (int i=0;i<infos.size();i++) {
            ArrayList<BeiDanChildBean> duoTypeInfoData;
            if(mapInfoData.get(String.valueOf(i)) == null){
                duoTypeInfoData = new ArrayList<>();
            }else{
                duoTypeInfoData = mapInfoData.get(String.valueOf(i));
            }

            BeiDanChildBean infor = infos.get(i);
            BeiDanChildBean inforSpf = null;
            BeiDanChildBean inforRspf = null;
            BeiDanChildBean inforBqc = null;
            BeiDanChildBean inforBf = null;
            BeiDanChildBean inforZjq = null;

            String[] keyArray_spf = {infor.VS_3, infor.VS_1, infor.VS_0};
            String[] keyArray_rspf = {infor.LETVS_3, infor.LETVS_1, infor.LETVS_0};
            String[] keyArray_bqc = {
                    infor.BQC_33, infor.BQC_31, infor.BQC_30, infor.BQC_13,
                    infor.BQC_11, infor.BQC_10, infor.BQC_03, infor.BQC_01,
                    infor.BQC_00};
            String[] keyArray_bf = {infor.BF_10, infor.BF_20, infor.BF_21,
                    infor.BF_30, infor.BF_31, infor.BF_32,
                    infor.BF_40, infor.BF_41, infor.BF_42,
                    infor.BF_50, infor.BF_51, infor.BF_52,
                    infor.BF_90, infor.BF_00, infor.BF_11,
                    infor.BF_22, infor.BF_33, infor.BF_99,
                    infor.BF_01, infor.BF_02, infor.BF_12, infor.BF_03,
                    infor.BF_13, infor.BF_23, infor.BF_04,
                    infor.BF_14, infor.BF_24, infor.BF_05,
                    infor.BF_15, infor.BF_25, infor.BF_09};
            String[] keyArray_jqc = {infor.SCORE_0, infor.SCORE_1, infor.SCORE_2, infor
                    .SCORE_3,
                    infor.SCORE_4, infor.SCORE_5, infor.SCORE_6, infor.SCORE_7};

            for (String key : keyArray_spf) {
                BeiDanChildBean.SPInfo spInfo = infor.btnHGPojo.get(key);
                if (spInfo != null && spInfo.isSel) {
                    inforSpf = clone(infor);
                    break;
                }
            }
            for (String key : keyArray_rspf) {
                BeiDanChildBean.SPInfo spInfo = infor.btnHGPojo.get(key);
                if (spInfo != null &&spInfo.isSel) {
                    inforRspf = clone(infor);
                    break;
                }
            }
            for (String key : keyArray_bqc) {
                BeiDanChildBean.SPInfo spInfo = infor.btnHGPojo.get(key);
                if (spInfo != null &&spInfo.isSel) {
                    inforBqc = clone(infor);
                    break;
                }
            }
            for (String key : keyArray_bf) {
                BeiDanChildBean.SPInfo spInfo = infor.btnHGPojo.get(key);
                if (spInfo != null &&spInfo.isSel) {
                    inforBf = clone(infor);
                    break;
                }
            }
            for (String key : keyArray_jqc) {
                BeiDanChildBean.SPInfo spInfo = infor.btnHGPojo.get(key);
                if (spInfo != null &&spInfo.isSel) {
                    inforZjq = clone(infor);
                    break;
                }
            }

            if(inforSpf !=null ){
                for (String key : keyArray_rspf) {
                    BeiDanChildBean.SPInfo spInfo = inforSpf.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                for (String key : keyArray_bqc) {
                    BeiDanChildBean.SPInfo spInfo = inforSpf.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                for (String key : keyArray_bf) {
                    BeiDanChildBean.SPInfo spInfo = inforSpf.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                for (String key : keyArray_jqc) {
                    BeiDanChildBean.SPInfo spInfo = inforSpf.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                duoTypeInfoData.add(inforSpf);
            }

            if(inforRspf != null){
                for (String key : keyArray_spf) {
                    BeiDanChildBean.SPInfo spInfo = inforRspf.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                for (String key : keyArray_bqc) {
                    BeiDanChildBean.SPInfo spInfo = inforRspf.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                for (String key : keyArray_bf) {
                    BeiDanChildBean.SPInfo spInfo = inforRspf.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                for (String key : keyArray_jqc) {
                    BeiDanChildBean.SPInfo spInfo = inforRspf.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                duoTypeInfoData.add(inforRspf);
            }

            if(inforBqc != null){
                for (String key : keyArray_spf) {
                    BeiDanChildBean.SPInfo spInfo = inforBqc.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                for (String key : keyArray_rspf) {
                    BeiDanChildBean.SPInfo spInfo = inforBqc.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                for (String key : keyArray_bf) {
                    BeiDanChildBean.SPInfo spInfo = inforBqc.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                for (String key : keyArray_jqc) {
                    BeiDanChildBean.SPInfo spInfo = inforBqc.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                duoTypeInfoData.add(inforBqc);
            }

            if(inforBf != null){
                for (String key : keyArray_spf) {
                    BeiDanChildBean.SPInfo spInfo = inforBf.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                for (String key : keyArray_rspf) {
                    BeiDanChildBean.SPInfo spInfo = inforBf.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                for (String key : keyArray_bqc) {
                    BeiDanChildBean.SPInfo spInfo = inforBf.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                for (String key : keyArray_jqc) {
                    BeiDanChildBean.SPInfo spInfo = inforBf.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                duoTypeInfoData.add(inforBf);
            }

            if(inforZjq != null){
                for (String key : keyArray_spf) {
                    BeiDanChildBean.SPInfo spInfo = inforZjq.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                for (String key : keyArray_rspf) {
                    BeiDanChildBean.SPInfo spInfo = inforZjq.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                for (String key : keyArray_bqc) {
                    BeiDanChildBean.SPInfo spInfo = inforZjq.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                for (String key : keyArray_bf) {
                    BeiDanChildBean.SPInfo spInfo = inforZjq.btnHGPojo.get(key);
                    if (spInfo != null && spInfo.isSel) {
                        spInfo.isSel = false;
                    }
                }
                duoTypeInfoData.add(inforZjq);
            }


            mapInfoData.put(String.valueOf(i), duoTypeInfoData);

            LogUtils.e("====duoTypeInfoData===="+duoTypeInfoData.toString());
            LogUtils.e("====mapInfoData===="+mapInfoData.toString());
        }
        return mapInfoData;
    }

    public static List<ArrayList<BeiDanChildBean>> convert(ArrayList<BeiDanChildBean> infos, int teamNum) {
        Map<String, ArrayList<BeiDanChildBean>> mapInfoData = getMapInfoData(infos);
        List<Map<String, List<BeiDanChildBean>>> splitMapInfoDataNum = splitMapInfoDataNum(mapInfoData, teamNum);
        List<ArrayList<BeiDanChildBean>> listMatchs = convertToStandardMapInfoData(splitMapInfoDataNum);
        return listMatchs;
    }

    public static List<Map<String,List<BeiDanChildBean>>> splitMapInfoDataNum(Map<String, ArrayList<BeiDanChildBean>> mapInfoData, int teamNum) {
        Set<String> keySet = mapInfoData.keySet();
        List<String> list = new ArrayList<String>();

        for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();) {
            String string = iterator.next();
            list.add(string);
        }

        List<List<String>> split = JZBetNumUtil.getCodeCollection(list, teamNum);

        List<Map<String, List<BeiDanChildBean>>> listMap = new ArrayList<Map<String, List<BeiDanChildBean>>>();
        for(List<String> strings:split) {
            Map<String, List<BeiDanChildBean>> map = new HashMap<String,List<BeiDanChildBean>>();
            for(String match : strings) {
                map.put(match, mapInfoData.get(match));
            }
            listMap.add(map);
        }
        LogUtils.e("====listBap===="+listMap.toString());
        return listMap;
    }

    public static List<ArrayList<BeiDanChildBean>> convertToStandardMapInfoData(List<Map<String, List<BeiDanChildBean>>> listMap) {
        List<ArrayList<BeiDanChildBean>> listMatchs = new ArrayList<ArrayList<BeiDanChildBean>>();
        for(Map<String, List<BeiDanChildBean>> map : listMap) {
            List<ArrayList<BeiDanChildBean>> listlist = new ArrayList<ArrayList<BeiDanChildBean>>();
            for (Iterator<String> iterator = map.keySet().iterator(); iterator.hasNext();) {
                String changci = iterator.next();
                List<BeiDanChildBean> changcicode = map.get(changci);
                listlist = rezuheList(listlist, changcicode);
            }
            listMatchs.addAll(listlist);
        }
        return listMatchs;
    }

    public static List<ArrayList<BeiDanChildBean>> rezuheList(List<ArrayList<BeiDanChildBean>> listOri, List<BeiDanChildBean> as) {
        List<ArrayList<BeiDanChildBean>> listlist = new ArrayList<ArrayList<BeiDanChildBean>>();
        if(listOri.size()==0) {
            for(BeiDanChildBean a : as) {
                ArrayList<BeiDanChildBean> list = new ArrayList<BeiDanChildBean>();
                list.add(a);
                listlist.add(list);
            }
            return listlist;
        }

        for(List<BeiDanChildBean> list : listOri) {
            for(BeiDanChildBean a : as) {
                ArrayList<BeiDanChildBean> newlist = new ArrayList<BeiDanChildBean>();
                newlist.addAll(list);
                newlist.add(a);
                listlist.add(newlist);
            }
        }
        return listlist;
    }

    public static void initSPFInfos(ArrayList<BeiDanChildBean> jcInfos) {
        for (BeiDanChildBean jcInfo : jcInfos) {
            if (jcInfo.checkNum > 1) {
                int handicap = Integer.valueOf(jcInfo.getHandicap());
                Map<String, BeiDanChildBean.SPInfo> btnHGPojo = jcInfo.btnHGPojo;
                BeiDanChildBean.SPInfo vs_3 = btnHGPojo.get("vs_3");
                BeiDanChildBean.SPInfo vs_1 = btnHGPojo.get("vs_1");
                BeiDanChildBean.SPInfo vs_0 = btnHGPojo.get("vs_0");
                BeiDanChildBean.SPInfo letvs_3 = btnHGPojo.get("letvs_3");
                BeiDanChildBean.SPInfo letvs_1 = btnHGPojo.get("letvs_1");
                BeiDanChildBean.SPInfo letvs_0 = btnHGPojo.get("letvs_0");
                List<SpValueBean> spValueList = new ArrayList<SpValueBean>();
                if (handicap < 0) {
                    if (handicap == -1) {
                        if (vs_3.isSel) {
                            if (letvs_1.isSel) {
                                spValueList.add(new SpValueBean("vs_3~letvs_1", Double.valueOf(vs_3.spValue) + Double.valueOf(letvs_1.spValue)));
                            }
                            if (letvs_3.isSel) {
                                spValueList.add(new SpValueBean("vs_3~letvs_3", Double.valueOf(vs_3.spValue) + Double.valueOf(letvs_3.spValue)));
                            }
                            spValueList.add(new SpValueBean("vs_3", Double.valueOf(vs_3.spValue)));
                        }
                        if (vs_1.isSel) {
                            if (letvs_0.isSel) {
                                spValueList.add(new SpValueBean("vs_1~letvs_9", Double.valueOf(vs_1.spValue) + Double.valueOf(letvs_0.spValue)));
                            }
                            spValueList.add(new SpValueBean("vs_1", Double.valueOf(vs_1.spValue)));
                        }
                        if (vs_0.isSel) {
                            if (letvs_0.isSel) {
                                spValueList.add(new SpValueBean("vs_0~letvs_0", Double.valueOf(vs_0.spValue) + Double.valueOf(letvs_0.spValue)));
                            }
                            spValueList.add(new SpValueBean("vs_0", Double.valueOf(vs_0.spValue)));
                        }

                    } else {//<-1
                        if (vs_3.isSel) {
                            if (letvs_1.isSel) {
                                spValueList.add(new SpValueBean("vs_3~letvs_1", Double.valueOf(vs_3.spValue) + Double.valueOf(letvs_1.spValue)));
                            }
                            if (letvs_3.isSel) {
                                spValueList.add(new SpValueBean("vs_3~letvs_3", Double.valueOf(vs_3.spValue) + Double.valueOf(letvs_3.spValue)));
                            }
                            if (letvs_0.isSel) {
                                spValueList.add(new SpValueBean("vs_3~letvs_0", Double.valueOf(vs_3.spValue) + Double.valueOf(letvs_0.spValue)));
                            }
                            spValueList.add(new SpValueBean("vs_3", Double.valueOf(vs_3.spValue)));
                        }
                        if (vs_1.isSel) {
                            if (letvs_0.isSel) {
                                spValueList.add(new SpValueBean("vs_1~letvs_0", Double.valueOf(vs_1.spValue) + Double.valueOf(letvs_0.spValue)));
                            }
                            spValueList.add(new SpValueBean("vs_1", Double.valueOf(vs_1.spValue)));
                        }
                        if (vs_0.isSel) {
                            if (letvs_0.isSel) {
                                spValueList.add(new SpValueBean("vs_0~letvs_0", Double.valueOf(vs_0.spValue) + Double.valueOf(letvs_0.spValue)));
                            }
                            spValueList.add(new SpValueBean("vs_0", Double.valueOf(vs_0.spValue)));
                        }
                    }
                } else {
                    if (handicap == 1) {
                        if (vs_3.isSel) {
                            if (letvs_3.isSel) {
                                spValueList.add(new SpValueBean("vs_3~letvs_3", Double.valueOf(vs_3.spValue) + Double.valueOf(letvs_3.spValue)));
                            }
                            spValueList.add(new SpValueBean("vs_3", Double.valueOf(vs_3.spValue)));
                        }
                        if (vs_1.isSel) {
                            if (letvs_3.isSel) {
                                spValueList.add(new SpValueBean("vs_1~letvs_3", Double.valueOf(vs_1.spValue) + Double.valueOf(letvs_3.spValue)));
                            }
                            spValueList.add(new SpValueBean("vs_1", Double.valueOf(vs_1.spValue)));
                        }
                        if (vs_0.isSel) {
                            if (letvs_0.isSel) {
                                spValueList.add(new SpValueBean("vs_0~letvs_0", Double.valueOf(vs_0.spValue) + Double.valueOf(letvs_0.spValue)));
                            }
                            if (letvs_1.isSel) {
                                spValueList.add(new SpValueBean("vs_0~letvs_1", Double.valueOf(vs_0.spValue) + Double.valueOf(letvs_1.spValue)));
                            }
                            spValueList.add(new SpValueBean("vs_0", Double.valueOf(vs_0.spValue)));
                        }
                    } else {//>1
                        if (vs_3.isSel) {
                            if (letvs_3.isSel) {
                                spValueList.add(new SpValueBean("vs_3~letvs_3", Double.valueOf(vs_3.spValue) + Double.valueOf(letvs_3.spValue)));
                            }
                            spValueList.add(new SpValueBean("vs_3", Double.valueOf(vs_3.spValue)));
                        }
                        if (vs_1.isSel) {
                            if (letvs_3.isSel) {
                                spValueList.add(new SpValueBean("vs_1~letvs_3", Double.valueOf(vs_1.spValue) + Double.valueOf(letvs_3.spValue)));
                            }
                            spValueList.add(new SpValueBean("vs_1", Double.valueOf(vs_1.spValue)));
                        }
                        if (vs_0.isSel) {
                            if (letvs_0.isSel) {
                                spValueList.add(new SpValueBean("vs_0~letvs_0", Double.valueOf(vs_0.spValue) + Double.valueOf(letvs_0.spValue)));
                            }
                            if (letvs_1.isSel) {
                                spValueList.add(new SpValueBean("vs_0~letvs_1", Double.valueOf(vs_0.spValue) + Double.valueOf(letvs_1.spValue)));
                            }
                            if (letvs_3.isSel) {
                                spValueList.add(new SpValueBean("vs_0~letvs_3", Double.valueOf(vs_0.spValue) + Double.valueOf(letvs_3.spValue)));
                            }
                            spValueList.add(new SpValueBean("vs_0", Double.valueOf(vs_0.spValue)));
                        }

                    }
                }
                if (letvs_0.isSel) {
                    spValueList.add(new SpValueBean("letvs_0", Double.valueOf(letvs_0.spValue)));
                }
                if (letvs_1.isSel) {
                    spValueList.add(new SpValueBean("letvs_1", Double.valueOf(letvs_1.spValue)));
                }
                if (letvs_3.isSel) {
                    spValueList.add(new SpValueBean("letvs_3", Double.valueOf(letvs_3.spValue)));
                }
                if (spValueList.isEmpty()){
                    continue;
                }
                Collections.sort(spValueList);
                SpValueBean spValueBean = spValueList.get(spValueList.size() - 1);
                String style = spValueBean.getStyle();
                String[] split = style.split("~");
                jcInfo.checkNum = split.length;
                Iterator it = btnHGPojo.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry entry = (Map.Entry) it.next();
                    String key = (String) entry.getKey();
                    BeiDanChildBean.SPInfo value = (BeiDanChildBean.SPInfo) entry.getValue();
                    if (split.length > 1) {
                        if (!key.equals(split[0]) && !key.equals(split[1])) {
                            value.isSel = false;
                        }
                    } else {
                        if (!key.equals(split[0])) {
                            value.isSel = false;
                        }
                    }
                }

            }

        }
    }

    public static void initHGInfos(ArrayList<BeiDanChildBean> hgInfos) {
        for (BeiDanChildBean jcInfo : hgInfos) {
            int teamNum = jcInfo.checkNum;
            if (teamNum > 0) {
                List<SpValueBean> spValueList = getHGMapMax(jcInfo);

                LogUtils.e("====spValueList===="+spValueList.toString());

                if (spValueList.isEmpty()){
                    continue;
                }

                Collections.sort(spValueList);
                SpValueBean spValueBean = spValueList.get(spValueList.size() - 1);
                String style = spValueBean.getStyle();

                String[] split = style.split("~");
                List<String> splitList = Arrays.asList(split);

                jcInfo.checkNum = split.length;
                Iterator it = jcInfo.btnHGPojo.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry entry = (Map.Entry) it.next();
                    String key = (String) entry.getKey();
                    BeiDanChildBean.SPInfo value = (BeiDanChildBean.SPInfo) entry.getValue();
                    if (!splitList.contains(key)) {
                        value.isSel = false;
                    }
                }
            }
        }

    }

    private static List<SpValueBean> getHGMapMax(BeiDanChildBean jcInfo) {
        List<SpValueBean> spValueList = new ArrayList<>();
        List<String[]> mixList = new ArrayList<>();
        BdHGGroup JZHGGroup = new BdHGGroup(jcInfo);
        List<StringArrayObj> hg = JZHGGroup.getAll();

        for (StringArrayObj stringArrayObj : hg) {
            mixList.add(stringArrayObj.tagArr);
        }

        Map<String, Double> maxSpValueMap = new HashMap<>();
        double[] pls = new double[mixList.size()];
        int x = 0;

        for (String[] arr : mixList) {
            StringBuffer key = new StringBuffer();
            double item = 0.0d;
            for (String str : arr) {
                key.append(str).append("~");
                item += Double.parseDouble(jcInfo.btnHGPojo.get(str).spValue);
                maxSpValueMap.put(key.toString(), item);
            }
            pls[x] = item;
            x++;
        }

        Arrays.sort(pls);
        double max = pls[pls.length - 1];

        if (pls.length == 0) {
            LogUtils.e("计算混关预计奖金异常……");
            return spValueList;
        }

        for(String key : maxSpValueMap.keySet()){
            if(max == maxSpValueMap.get(key)){
                spValueList.add(new SpValueBean(key, maxSpValueMap.get(key)));
                break;
            }
        }

        return spValueList;
    }

}
